
package finalproject.server;


import java.util.List;
import javax.jdo.PersistenceManager;

import finalproject.server.PmfSingleton;
import finalproject.shared.Vak;


public class VakDAOPmf implements VakDAO {

	
	public void addVak(Vak vak) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		try {
			pm.makePersistent(vak);
		} finally {
			pm.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<Vak> listVakken() {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		String query = "select from " + Vak.class.getName();
		return (List<Vak>) pm.newQuery(query).execute();
	}

	
	public void removeVak(Vak vak) {
		PersistenceManager pm =PmfSingleton.get().getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			vak = pm.getObjectById(Vak.class, vak.getVakNr());
			pm.deletePersistent(vak);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	public void updateVak(Vak vak) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		
		try {
			
			Vak vakOld = pm.getObjectById(Vak.class, vak.getVakNr());
			pm.deletePersistent(vakOld);

			
			pm.makePersistent(vak);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

}
